use serde::de::Visitor;
use std::fmt;
use std::fmt::Formatter;
use super::log_level::LogLevel;

pub struct LogLevelVisitor;

impl<'de> Visitor<'de> for LogLevelVisitor {
  type Value = LogLevel;

  fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
    formatter.write_str("a string representing a LogLevel")
  }

  fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> where E: serde::de::Error {
    LogLevel::from_str(v).map_err(|e: String| {
      E::custom(format!("invalid LogLevel: {}", e))
    })
  }
}
